package com.bigdata.mapreduce.avg;

import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.util.Iterator;

public class WordReducer extends Reducer<Text, Text, Text, Text> {

//    private DoubleWritable result = new DoubleWritable();
    private Text result = new Text();

    @Override
    protected void reduce(Text key, Iterable<Text> values, Reducer<Text, Text, Text, Text>.Context context) throws IOException, InterruptedException {
        int sum = 0;
        int count = 0;
        for (Text value : values) {
            String[] paramArr = value.toString().split("_");
            sum += Integer.valueOf(paramArr[0]);
            count += Integer.valueOf(paramArr[1]);
        }
        double aver = (double) sum / count;
        result.set(aver + "");
        context.write(key, result);
    }
}
